package main

import (
	"fmt"
	"net/http"
	"net/http/httputil"
	"sort"
)

func main() {
	request, err := http.NewRequest(http.MethodGet, "http://www.imooc.com", nil)
	request.Header.Add("User-Agent", "Mozilla/5.0 (iPhone; CPU iPhone OS 13_2_3 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.3 Mobile/15E148 Safari/604.1")
	// 自定义 Client
	client := http.Client{
		CheckRedirect: func(
			req *http.Request,
			via []*http.Request) error {
			fmt.Println("Redirect: ", req)
			return nil
		},
	}
	resp, err := client.Do(request)
	//resp, err := http.DefaultClient.Do(request)
	//resp, err := http.Get("http://www.imooc.com")

	if err != nil {
		panic(err)
	}
	defer resp.Body.Close()

	s, err := httputil.DumpResponse(resp, true)
	if err != nil {
		panic(err)
	}

	fmt.Printf("%s\n", s)
}


func combinationSum(candidates []int, target int) [][]int {
	sort.Ints(candidates)
	return combine(candidates, target)
}

func combine(nums []int, target int) [][]int {
	var res [][]int
	if len(nums) <= 0 { // 递归到最后一个元素，递归结束
		return res
	}

	subSum := target - nums[0]

	switch {
	case subSum < 0: // 向后不存在更大的值，递归结束
		return res
	case subSum == 0: // 第一个数就是 target
		res = append(res, []int{nums[0]})
	case subSum > 0:
		remains := combine(nums, subSum) // 寻找所有的子和组合
		for _, v := range remains {
			way := append([]int{nums[0]}, v...)
			res = append(res, way)
		}
	}

	res = append(res, combine(nums[1:], target)...) // 向后查找其他组合，避免重复
	return res
}
